package com.nbicc.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.nbicc.pojo.Finan;
import com.nbicc.pojo.Record;
import com.nbicc.service.FinanService;
import com.nbicc.service.RecordService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/5/12 15:08
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {
    @Autowired
    private FinanService finanService;
    @Autowired
    private RecordService recordService;

    /**
     * Finan:现金券发券记录导入
     * @param file
     * @return ok
     * @throws Exception 全部异常
     */
    @RequestMapping("/importFinan")
    public String importFinan(MultipartFile file) throws Exception {
        System.out.println("--importFinan--");
        String originalFilename = file.getOriginalFilename();
        ImportParams params = new ImportParams();
        //标题几行
        params.setTitleRows(1);
        //表头几行
        params.setHeadRows(1);
        params.setNeedVerify(true);
        List<Finan> list = ExcelImportUtil.importExcel(file.getInputStream(), Finan.class, params);
        list.forEach(a -> {
            finanService.insertFinan(a);
        });
        list.forEach(System.out::println);
        return "ok";
    }

    /**
     * 现金券充值记录的导入
     * @param file
     * @return ok
     * @throws Exception 全部异常
     */
    @RequestMapping("/importRecord")
    public String excelFiles(MultipartFile file) throws Exception {
        System.out.println("--importRecord--");
        String originalFilename = file.getOriginalFilename();
        ImportParams params = new ImportParams();
        //标题几行
        params.setTitleRows(1);
        //表头几行
        params.setHeadRows(1);
        params.setNeedVerify(true);
        List<Record> list = ExcelImportUtil.importExcel(file.getInputStream(), Record.class, params);
        list.forEach(a -> {
            recordService.insertRecord(a);
        });
        list.forEach(System.out::println);
        return "ok";
    }

    /**
     * 现金券发券记录导出
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/exportFinan")
    public String exportFinan(HttpServletResponse response) throws IOException {
        System.out.println("--exportFinan--");
        List<Finan> finans = finanService.selectAllExcel();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("现金券发券记录", "sheet1"), Finan.class, finans);
        response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("现金券发券明细.xls"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
        return "ok";
    }

    /**
     * 现金券充值记录导出
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/exportRecord")
    public String exportRecord(HttpServletResponse response) throws IOException {
        System.out.println("--exportRecord--");
        List<Record> records = recordService.selectAllExcel();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("现金券充值使用记录", "sheet1"), Record.class, records);
        response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("现金券充值记录.xls"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
        return "ok";
    }
}
